﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ra2_jifenban
{
    public partial class Form1 : Form
    {
        private int x, y;
        private Config Config = new Config();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 隐藏标题栏
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

            this.MouseDown += Form1_MouseDown;
            this.MouseMove += Form1_MouseMove;
            this.FormClosing += Form1_FormClosing;
            loadConfig();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Config.LocationX = this.Bounds.X;
            Config.LocationY = this.Bounds.Y;
        }

        private void loadConfig()
        {
            this.SetBounds(Config.LocationX, Config.LocationY, Config.Width, Config.Heigth);
            this.TopMost = true;
            reloadWindow();
        }

        private void reloadWindow()
        {
            //this.Width = Config.Width;
            //this.Height = Config.Heigth;
            this.BackColor = getColorFromRGBString(Config.BackColor);
            int boudsX = Config.AutoCenter ? (Screen.PrimaryScreen.WorkingArea.Width - Config.Width) / 2 : this.Bounds.X;
            this.SetBounds(boudsX, this.Bounds.Y, Config.Width, Config.Heigth);

            if (File.Exists(Config.BackgroundImage))
            {
                this.BackgroundImage = System.Drawing.Image.FromFile(Config.BackgroundImage);
                this.TransparencyKey = this.BackColor; // Color.White;
                this.Opacity = Config.Opacity / 100;
            }

            int lx1 = (Config.Width - this.label1.Width) / 2;
            int ly1 = (Config.Heigth - this.label1.Height) / 2;
            this.label1.SetBounds(lx1, ly1, this.label1.Width, this.label1.Height);

            int lx2 = (Config.Width - this.label1.Width) / 2 - this.label2.Width;
            int ly2 = (Config.Heigth - this.label2.Height) / 2;
            this.label2.SetBounds(lx2, ly2, this.label2.Width, this.label2.Height);
            this.label2.Text = $"{Config.PlayerA} {Config.ScoreA}";

            int lx3 = (Config.Width - this.label1.Width) / 2 + this.label1.Width / 2 + 24;
            int ly3 = (Config.Heigth - this.label3.Height) / 2;
            this.label3.SetBounds(lx3, ly3, this.Width, this.label3.Height);
            this.label3.Text = $"{Config.ScoreB} {Config.PlayerB}";

            this.label1.ForeColor = getColorFromRGBString(Config.FontColor);
            this.label2.ForeColor = getColorFromRGBString(Config.FontColor);
            this.label3.ForeColor = getColorFromRGBString(Config.FontColor);
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + (e.X - x), this.Location.Y + (e.Y - y));
            }
        }

        private void openConfigFile()
        {
            // 查看/修改 配置文件
            Process p = new Process();
            p.StartInfo.UseShellExecute = true;

            string textEditor = Config.TextEditor;
            if (Path.IsPathRooted(textEditor))
            {
                FileInfo fileInfo = new FileInfo(textEditor);
                p.StartInfo.WorkingDirectory = fileInfo.Directory.FullName;
                p.StartInfo.FileName = fileInfo.Name;
            }
            else
            {
                p.StartInfo.FileName = textEditor;
            }

            p.StartInfo.Arguments = Config.ConfigFile;

            p.Start();

            p.WaitForExit();
        }

        private void label1_MouseClick(object sender, MouseEventArgs e)
        {
            // 左键打开配置文件
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                openConfigFile();
            }
            // 右键打开置零比分
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                Config.ScoreA = Config.ScoreB = 0;
            }
            reloadWindow();
        }

        private void label2_MouseClick(object sender, MouseEventArgs e)
        {
            // 修改A比分
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Config.ScoreA++;
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                Config.ScoreA--;
            }
            reloadWindow();
        }

        private void label3_MouseClick(object sender, MouseEventArgs e)
        {
            // 修改B比分
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Config.ScoreB++;
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                Config.ScoreB--;
            }
            reloadWindow();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                x = e.X;
                y = e.Y;
            }
        }

        private Color getColorFromRGBString(string rgb)
        {
            return getColorFromRGBString(rgb, Color.Transparent);
        }

        private Color getColorFromRGBString(string rgb, Color defaultIfError)
        {
            string[] rgbArr = rgb.Split(',');
            try
            {
                int r = int.Parse(rgbArr[0].Trim());
                int g = int.Parse(rgbArr[1].Trim());
                int b = int.Parse(rgbArr[2].Trim());
                return Color.FromArgb(r, g, b);
            }
            catch
            {
                return defaultIfError;
            }
        }
    }
}
